package cn.hughie.imooc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author yuh
 * @Date Created in 上午11:43 2018/5/26
 * @Description 重写方法后不再有同步synchronized
 * volitate 适用于只有一个线程写的时候 或者说 写的时候不依赖当前值
 * 不要在构造函启动任何线程 或者调用任何方法 只能是进行构造 否则线程会见到还未构造完全的this引用
 */
public class Parent {

    public synchronized void test1() {
        System.out.println(this);
        for (int i = 0; i < 10; i++) {
            System.out.println("test1 " + i);
        }
    }

    public synchronized void test2() {
        for (int i = 0; i < 10; i++) {
            System.out.println("test2 " + i);
        }
    }
}

class Sub extends Parent {

    @Override
    public void test1() {
        System.out.println(this);
        for (int i = 0; i < 10; i++) {
            System.out.println("test1 " + i);
        }
    }

    @Override
    public void test2() {
        for (int i = 0; i < 10; i++) {
            System.out.println("test2 " + i);
        }
    }


    public static void main(String[] args) {
        Sub sub = new Sub();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> sub.test1());
        executorService.execute(() -> sub.test2());
    }
}


